package com.cheng.controller;

import com.cheng.common.exception.CommonException;
import com.cheng.common.results.DataRslt;
import com.cheng.data.vo.UserInfoVO;
import com.cheng.operation.UserOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.UUID;

/**
 * @author: ChengJW
 * @date: 2021/11/20 8:12
 * @version: 1.0
 * @description: 用户信息处理类，包括修改头像、修改密码、修改用户信息
 */
@Controller
public class UserInfoController extends BaseController{

    private final static Logger log = LoggerFactory.getLogger(UserInfoController.class);

    @Autowired
    private UserOperation userOperation;

    @RequestMapping("/userinfo/updateProfilePhoto")
    public String updateProfilePhoto(){
        return "updateProfilePhoto";
    }

    @RequestMapping("/userinfo/updatePassword")
    public String updatePassword(){
        return "updatePassword";
    }


    /**
    * @author: ChengJW
    * @params: []
    * @return: com.cheng.common.results.DataRslt
    * @date: 2021/11/20
    * @description: 修改密码
    */
    @RequestMapping("/userinfo/updatePassword/updatePswd")
    @ResponseBody
    public DataRslt updatePswd(UserInfoVO userInfoVO) throws CommonException{
        userInfoVO.setUrNm(getUrNm());
        userOperation.updateUserInfo(userInfoVO,"02");
        return success();
    }

    /**
    * @author: ChengJW
    * @params: [file]
    * @return: com.cheng.common.results.DataRslt
    * @date: 2021/11/23
    * @description: 上传头像
    */
    @RequestMapping("/userinfo/updateUserInfo/uploadPhoto")
    @ResponseBody
    public DataRslt uploadPhoto(MultipartFile file) throws CommonException{
        String photoPath = userOperation.uploadPhoto(file);
        return success(photoPath);
    }

    /**
    * @author: ChengJW
    * @params:
    * @return:
    * @date: 2021/11/24
    * @description: 回显头像
    */

    @RequestMapping("/userinfo/updateUserInfo/showPhoto")
    @ResponseBody
    public DataRslt showPhoto() throws CommonException{

        UserInfoVO userInfoVO = new UserInfoVO();
        userInfoVO.setUrNm(getUrNm());
        UserInfoVO vo = userOperation.queryUserInfo(userInfoVO);
        return success(vo.getUrPhotoPath());
    }

    /**
    * @author: ChengJW
    * @params: [userInfoVO]
    * @return: com.cheng.common.results.DataRslt
    * @date: 2021/11/24
    * @description: 修改用户头像
    */
    @RequestMapping("/userinfo/updateUserInfo/checkPhoto")
    @ResponseBody
    public DataRslt checkPhoto(UserInfoVO userInfoVO) throws CommonException{

        userInfoVO.setUrNm(getUrNm());
        userOperation.updateUserInfo(userInfoVO,"01");
        return success();
    }
}
